//
//  AfanUserAccount.swift
//  weibo
//
//  Created by afan on 16/3/26.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit

class AfanUserAccount: NSObject,NSCoding {
    
    /// 用于调用access_token，接口获取授权后的access token
    var access_token: String?
    
    /// access_token的生命周期，单位是秒数

    // 基本数据类型作为属性不能为可选,使用一个默认值
    var expires_in: NSTimeInterval = 0 {
        didSet {
            // 当kKVC给expires_in赋值完成后将expires_in转成NSDate
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    /// 过期时间,expires_in转成系统的NSDate,expires_in是Double不好来判断过期时间,转成NSDate比较好判断过期时间
    var expiresDate: NSDate?

    /// 当前授权用户的UID
    var uid: String?
    
    
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    /// 字典的 key 找不到模型对应属性时会崩.要实现这个办法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
    
    //MARK: 用户信息
    ///用户名
    var screen_name: String?
    ///用户头像地址
    var avatar_large: String?
    
        
    /// 对象打印
    override var description: String {
        // 只读计算性属性
        get {
            let keys = ["access_token", "expires_in", "expiresDate", "uid"]
            // 拿着传入的key去模型中找对应的属性,找到属性的值后会将key和属性的值,拼接成字典
            return dictionaryWithValuesForKeys(keys).description
        }
    }
    
    
    
    
    
    
    
    
    //MARK: 解档与归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large")  as? String
    }
}























